package fw.protocol.zigbee.bundle.activator;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

import es.unizar.howlab.core.io.serial.logic.SerialDriver;
import es.unizar.howlab.core.zigbee.telegesis.driver.Driver;
import fw.protocol.zigbee.bundle.DriverManager;
import fw.protocol.zigbee.bundle.SerialDriverManager;

public class Activator implements BundleActivator {

	private static BundleContext context;
	private ServiceTracker st = null;
	private SerialDriverManager sdm;
	private DriverManager dm;

	static BundleContext getContext() {
		return context;
	}

	/**
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext bc) throws Exception {
		System.out.println("Sraaaaaaaaaaaaaa");
		Activator.context = bc;
//		if (this.stZigbeeDevices == null) {
//			this.stZigbeeDevices = new ServiceTracke	r(this.context,
//					ZigbeeDevice.class.getName(), this);
//			this.stZigbeeDevices.open();
//		}
		sdm = new SerialDriverManager(bc);
        st = new ServiceTracker(bc, SerialDriver.class.getName(), sdm);
        st.open();
        
		dm = new DriverManager(bc);
        st = new ServiceTracker(bc, Driver.class.getName(), dm);
        st.open();
        
	}

	/**
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext bundleContext) throws Exception {
		Activator.context = null;
	}

//
//	private HashMap<ServiceReference, ZigbeeDevice> zigbeeDevices = new HashMap();
//	 
//	private static EmptyPayloadCommand CMD_ON = new EmptyPayloadCommand()
//	.setId(OnOff.ON_ID)
//	.setClientServerDirection(true)
//	.setClusterSpecific(true)
//	.setManufacturerExtension(false);
//	
//	@Override
//	public Object addingService(ServiceReference reference) {
//		System.out.println("New reference: " + reference);
//		SerialDriver sd = (SerialDriver) context.getService(reference);
//		sd.newSerialConnection(Protocol.rs232,  "COM4", "19200");
////		if (!this.zigbeeDevices.containsKey(reference)) {
////			ZigbeeDevice device = (ZigbeeDevice) this.context
////					.getService(reference);
////			// if
////			// (this.driver.getNode(device.getZigbeeNode().getEUI64Addr().toString())
////			// == null) {
////			// return null;
////			// }
//////
////			device.registerListener(this);
////			OnOffCluster on = new OnOffCluster(null);
////			OnOffImpl onOff = new OnOffImpl(null);
////			ZCLFrame inFrame = new ZCLFrame(CMD_ON, true);
////			Cluster input = new ClusterImpl((short) 0,inFrame);
////			this.zigbeeDevices.put(reference, device);
////			device.sendMessage(device.getProfileId(), device.getDeviceId(), input.getClusterMsg());
////			return device;
////		}
//		return null;
//	}
//
//	@Override
//	public void modifiedService(ServiceReference arg0, Object arg1) {
//		System.out.println("New reference:1 ");
//		// TODO Auto-generated method stub
//		
//	}
//
//	@Override
//	public void removedService(ServiceReference arg0, Object arg1) {
//		System.out.println("New reference:2 ");
//		// TODO Auto-generated method stub
//		
//	}

}
